package com.example.android2019.musices.musicservice;

import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.android2019.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * 1、获取Intent传递的参数
 * 2、修改TextView内容：Mp3文件名
 * 3、歌词内容的显示
 * 4、音乐的播放、暂停、继续、停止功能
 * 5、进度条的时间的显示
 * 6、进度条拖动事件
 */
public class MusicPlayActivity extends AppCompatActivity implements View.OnClickListener {
    /* 定义控件*/
    TextView tv_title, tv_lrc;
    ImageButton btn_play, btn_pause, btn_stop;
    SeekBar seekBar;
    TextView tv_progress, tv_total;

    /* 定义从intent获得的参数*/
    String mp3Name, sdPath;

    boolean is_play = false, is_pause = false, is_stop = false;
/*
    MediaPlayer mediaPlayer;

    @Override
    protected void onPause() {
        super.onPause();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer = null;
        }
    }*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_play);
        /* 关联布局文件中的控件*/
        init();
        /*1、获取Intent传递的参数 */
        Intent intent = this.getIntent();
        mp3Name = intent.getStringExtra("mp3Name");
        sdPath = intent.getStringExtra("sdPath");
        /*2、修改TextView内容：Mp3文件名*/
        tv_title.setText(mp3Name);
        lrc();

        btn_play.setOnClickListener(this);
        btn_pause.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
    }

    private void lrc() {
        String lrc = sdPath + "/" + mp3Name.substring(0, mp3Name.length() - 4) + ".lrc";
        File file = new File(lrc);
        FileInputStream in;
        BufferedReader br;
        try {
            in = new FileInputStream(file);
            br = new BufferedReader(new InputStreamReader(in));
            String line, s = "";
            while ((line = br.readLine()) != null) {
                s = s + line + "\n";
            }
            tv_lrc.setText(s);
            tv_lrc.setMovementMethod(ScrollingMovementMethod.getInstance());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

    private void init() {
        tv_title = findViewById(R.id.music_tv_title);
        tv_lrc = findViewById(R.id.music_tv_lyric);
        tv_progress = findViewById(R.id.music_tv_progress);
        tv_total = findViewById(R.id.music_tv_total);
        btn_play = findViewById(R.id.music_btn_play);
        btn_pause = findViewById(R.id.music_btn_pause);
        btn_stop = findViewById(R.id.music_btn_stop);
        seekBar = findViewById(R.id.music_seekbar);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.music_btn_play:
                if (!is_play) {
                    playMusic();
                }
                break;
            case R.id.music_btn_pause:
                if (is_play && !is_pause && !is_stop) {
                    pauseMusic();
                } else if (!is_play && is_pause && !is_stop) {
                    continueMusic();
                }
                break;
            case R.id.music_btn_stop:
                if (is_play && !is_stop) {
                    stopMusic();
                }
                break;
            default:
                break;
        }
    }

    private void stopMusic() {
        is_play = false;
        is_pause = false;
        is_stop = true;
        Intent intent = new Intent(this, MusicService.class);
        intent.putExtra("state", 4);
        startActivity(intent);
    }

    private void continueMusic() {
        /*mediaPlayer.start();*/
        is_play = true;
        is_pause = false;
        is_stop = false;
        Intent intent = new Intent(this, MusicService.class);
        intent.putExtra("state", 3);
        startActivity(intent);
    }


    private void pauseMusic() {
        /*mediaPlayer.pause();*/
        is_play = false;
        is_pause = true;
        is_stop = false;

        Intent intent = new Intent(this, MusicService.class);
        intent.putExtra("state", 2);
        startActivity(intent);
    }

    private void playMusic() {
        try {
           /* mediaPlayer.reset();
            mediaPlayer.setDataSource(sdPath + "/" + mp3Name);
            mediaPlayer.prepare();
            mediaPlayer.start();*/
            is_play = true;
            is_pause = false;
            is_stop = false;

            /*开启服务*/
            Intent intent = new Intent(this, MusicService.class);
            intent.putExtra("state", 1);
            intent.putExtra("path", sdPath + "/" + mp3Name);
            startActivity(intent);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}